package com.kgc.vo;

import com.alibaba.fastjson.JSON;

/**
 * 返回LayUI需要数据
 */
public class R<T> {

    private Integer code; // 0 成功 >1 失败
    private String msg; // 返回消息
    private Integer count; // 总条数
    private T data; // 返回数据

    /**
     * 默认成功
     *
     * @param <T>
     * @return
     */
    public static <T> R<T> success(String message, Integer count, T data) {
        return new R<T>(0, message, count, data);
    }

    /**
     * 默认成功
     *
     * @param <T>
     * @return
     */
    public static <T> R<T> success(String message, T data) {
        return new R<T>(0, message, 0, data);
    }
    public static <T> R<T> success( T data) {
        return new R<T>(0, "message", 0, data);
    }

    /**
     * 默认成功
     *
     * @param <T>
     * @return
     */
    public static <T> R<T> success(String message) {
        return new R<T>(0, message, 0, null);
    }

    /**
     * 返回失败数据
     *
     * @param message
     * @param <T>
     * @return
     */
    public static <T> R<T> error(String message) {
        return new R<>(1, message, 0, null);
    }

    /**
     * 返回失败数据
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> R<T> error(T data, String message) {
        return new R<>(1, message, 0, data);
    }



    public String toJSON() {
        return JSON.toJSONString(this);
    }







    public R() {
    }

    public R(Integer code, String msg, Integer count, T data) {
        this.code = code;
        this.msg = msg;
        this.count = count;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
